// utils/request.ts
// const baseUrl = 'https://127.0.0.1/'; // 测试环境
const baseUrl = 'https://admin.aitongfeng.com/'; //正式环境

// 定义请求配置的接口
interface RequestOptions {
	url : string;
	method ?: 'GET' | 'POST' | 'PUT' | 'DELETE';
	data ?: any;
}

// 封装请求方法
const request = (options : RequestOptions) : Promise<any> => {
	return new Promise((resolve, reject) => {
		uni.request({
			url: baseUrl + options.url,
			method: options.method || 'GET',
			data: options.data || {},
			header: {
				'Content-Type': 'application/json',
				'token': uni.getStorageSync('token')
				// 可以在这里添加其他请求头，如 token
				// 'Authorization': 'Bearer ' + uni.getStorageSync('token')
			},
			success: (res) => {
				if (res.statusCode === 200) {
					resolve(res.data);
				} else {
					reject(new Error(`请求失败，状态码：${res.statusCode}`));
				}
			},
			fail: (err) => {
				reject(err);
			}
		});
	});
};

// 封装 GET 请求
const get = (url : string, data ?: any) : Promise<any> => {
	return request({
		url,
		method: 'GET',
		data
	});
};

// 封装 POST 请求
const post = (url : string, data ?: any) : Promise<any> => {
	return request({
		url,
		method: 'POST',
		data
	});
};

// 封装 PUT 请求
const put = (url : string, data ?: any) : Promise<any> => {
	return request({
		url,
		method: 'PUT',
		data
	});
};

// 封装 DELETE 请求
const del = (url : string, data ?: any) : Promise<any> => {
	return request({
		url,
		method: 'DELETE',
		data
	});
};

export {
	get,
	post,
	put,
	del
};